package com.neuedu.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.neuedu.domain.Userinfo;
import com.neuedu.service.UserinfoService;
import com.neuedu.service.impl.UserinfoServiceImpl;

@WebServlet("/user/messageupdate")
public class UserMessageUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String update_email = request.getParameter("update_email");
		UserinfoService us = new UserinfoServiceImpl();
		Userinfo user = (Userinfo)request.getSession().getAttribute("login_user");
		int id = user.getId();
		Userinfo userid = us.upEmail(update_email);
		if(userid != null) {		
		if(userid.getId() == id) {
			response.getWriter().print(0);
		}else if(userid.getId() != id) {
			response.getWriter().print(1);
		}
		}else if(userid == null){
			response.getWriter().print(0);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String email = request.getParameter("email");
		String nickname = request.getParameter("username");
		String sex = request.getParameter("sex");
		String city = request.getParameter("city");
		String sign = request.getParameter("sign");
		Userinfo userinfo = new Userinfo();
		userinfo.setEmail(email);
		userinfo.setNickname(nickname);
		userinfo.setSex(sex);
		userinfo.setCity(city);
		userinfo.setSign(sign);
		Userinfo user = (Userinfo)request.getSession().getAttribute("login_user");
		int id = user.getId();
		userinfo.setId(id);
		UserinfoService us = new UserinfoServiceImpl();
		us.upMyMessage(userinfo);
		user.setEmail(email);
		user.setNickname(nickname);
		user.setSex(sex);
		user.setSign(sign);
		user.setCity(city);
		response.sendRedirect(request.getContextPath()+"/jsp/user/set.jsp");
	}

}
